<template>
	<view class="padding30rpx">
		<view class="login-font">
			<view>请设置新的密码</view>
		</view>
		<view class="hui66">
			重置之前的密码，设置新密码
		</view>
		<view>
			<view class="login-marg">密码</view>
			<view class="line" style="padding-bottom: 30rpx;">
				<u-input type="password" v-model="password" placeholder="请输入密码（6-12位数字或字母）" />
			</view>
		</view>
		<view>
			<view class="login-marg">确认密码</view>
			<view class="line" style="padding-bottom: 30rpx;">
				<u-input type="password" v-model="repassword" placeholder="请再次输入密码" />
			</view>
		</view>

		<button @tap="enter">确认更改</button>
	</view>
</template>

<script>
	export default {

		data() {
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.repassword) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				phone: '',
				password: '',
				repassword: '',
				show: true,
				// 表单校验
				rules: {
					userName: {
						rule: /\S/,
						msg: "账号不能为空"
					},
					password: {
						rule: /^[0-9a-zA-Z]{6,16}$/,
						msg: "密码应该为6-16位"
					},
					repassword: {
						rule: validatePass2,
						msg: "请确认密码"
					},
					userNamePhone: {
						rule: /^1[3456789]\d{9}$/,
						msg: "手机号格式错误"
					},
					userPwdPhone: {
						rule: /^[0-9]{6}$/,
						msg: "请输入6位数字验证码"
					}
				},
			}
		},
		onLoad(option) {
			this.phone = option.phone || ''
		},
		methods: {
			////判断验证是否符合要求，合法性校验
			validate(key) {
				let bool = true;
				if (!this.rules[key].rule.test(this[key])) {
					//提示信息
					uni.showToast({
						title: this.rules[key].msg,
					})
					//取反
					bool = false;
					return false;
				}
				return bool;
			},
			enter() {
				var vm = this
				if (!this.validate('password')) return;
				// if(!this.validate('repassword')) return;
				if (this.password !== this.repassword) {
					uni.showToast({
						icon: 'none',
						title: '两次输入密码不一致!'
					})
				}

				uni.request({
					method: 'GET',
					url: this.$api + '/app/UserController/updatePassword',
					data: {
						phone: this.phone || this.$store.state['userinfo']['phone'],
						password: this.password
					},
					header: {
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						console.log('', res.data)
						if (res.data.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '修改成功'
							})
							setTimeout(()=> {
								// uni.navigateBack({
								// 	delta: 1
								// });
								this.$store.commit('logout')
								uni.reLaunch({
									url: '/pagesB/login/login'
								})
							}, 1000);
						} else {
							this.$mHelper.toast(res.data.message)
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})




			},

		}
	}
</script>

<style lang="scss" scoped>
	button {
		margin-top: 256rpx;
		background-color: #00a29a;
		color: #FFFFFF;
		height: 92rpx;
		line-height: 92rpx;
		width: 100%;
	}

	input {
		font-size: 28rpx;
	}

	label {
		color: #007AFF;
	}

	.login-top {
		text-align: center;
		margin: 30rpx 0;

		image {
			width: 229rpx;
			height: 124rpx;

		}
	}

	.login-font {
		font-size: 40rpx;
		font-weight: 600;
		padding-bottom: 30rpx;
	}

	.login-marg {
		margin: 30rpx 0;
		font-size: 32rpx;
	}
</style>
